<template>
  <el-dialog
    title="添加好朋友"
    :visible.sync="Visible"
    width="35%"
    :before-close="closeForm"
  >
    <el-form
      ref="form"
      :model="form"
      label-width="120px"
      size="medium"
      style="margin-right: 40px"
    >
      <el-form-item label="好友ID">
        <el-input v-model="form.userId" :value="form.userId" />
      </el-form-item>

      <el-form-item label="好友名">
        <el-input v-model="form.nickname" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onAddSub">提交</el-button>
        <el-button @click="closeForm()">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { mapState } from "vuex";
import * as request from "@/utils/request";
export default {
  name: "Form",
  data() {
    return {
      form: {
        userId: "",
        nickname: "",
      },
    };
  },

  props: ["Visible"],
  computed: {
    ...mapState({
      // 当前用户信息
      myInfo: "myInfo",
    }),
  },
  methods: {
    closeForm() {
      this.$emit("closeForm");
    },
    onAddSub() {
      let that = this;

      request
        .postUrlContentType("http://localhost:8081/userFriend/addFriend", {
         // userId: this.myInfo.userId,
          userId: this.form.userId,
          nickname: this.form.nickname
        })
        .then(function (data) {
          console.log("添加朋友响应:",data);
          if (data.code == 200) {
            that.$notify({
              title: "添加成功",
              message: data.data,
              type: "success",
            });
            //要么重新请求一次，要么直接刷新当前页面
            location.reload();
          } else {
            that.$notify({
              title: "添加失败",
              message: data.message,
              type: "warning",
            });
          }
        });

      this.$emit("closeForm");
    },
  },
};
</script>
